package palmtop.com.palmtop.thread;


import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.UserHandle;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresPermission;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeResult;

import net.grandcentrix.tray.AppPreferences;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import palmtop.com.palmtop.activity.Main2Activity;
import palmtop.com.palmtop.util.Config;
import palmtop.com.palmtop.util.NetUtil;

/**
 * Created by Administrator on 2018/8/24.
 */

public final class PersonposThread  implements GeocodeSearch.OnGeocodeSearchListener{
    String latitude = "";
    String longitude = "";
    public AMapLocationClient mLocationClient = null;
    public AMapLocationClientOption mLocationOption = null;
    public static  boolean exit = false;
    Context context ;
    String ssgrid;
    String username;
    String userid;
    long time=System.currentTimeMillis()/1000;
    int count = 1;
    public PersonposThread(Context c)
    {
        this.context = c;
        AppPreferences appPreferences = new AppPreferences(context);
        userid = appPreferences.getString("userid", "");
        username = appPreferences.getString("name", "");
        ssgrid =appPreferences.getString("ssgrid", "");
    }


    public  static void run() {

//            while(!exit){
//                try {
//                    getGpsInfo();
//                    Thread.sleep(30000);

//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
            /*if(exit==true){
                exit = false;
            }*/




    }

    private void addPersonpos() {
        RequestParams params = new RequestParams(Config.SERVER_URL + "personpos/save");
        params.addBodyParameter("userid", userid);//任务内容
        params.addBodyParameter("username", username);//任务地点
        params.addBodyParameter("ssgridid", ssgrid);//具体内容
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sDateFormat.format(new Date());
        params.addBodyParameter("logintime", date);// 添加时间
        params.addBodyParameter("longitude", longitude);
        params.addBodyParameter("latitude", latitude);
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });
    }

    private void getGpsInfo() {
        mLocationClient = new AMapLocationClient(context);
        mLocationClient.setLocationListener(mLocationListener);
        mLocationOption = new AMapLocationClientOption();
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        mLocationOption.setNeedAddress(true);
        mLocationOption.setOnceLocation(true);
        mLocationClient.setLocationOption(mLocationOption);
        mLocationClient.startLocation();
    }

    public AMapLocationListener mLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation amapLocation) {
            if (amapLocation != null) {
                if (amapLocation.getErrorCode() == 0) {
                    latitude = amapLocation.getLatitude() + "";
                    longitude = amapLocation.getLongitude() + "";
                    addPersonpos();
                } else {
                    //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
                    Log.e("AmapError", "location Error, ErrCode:"
                            + amapLocation.getErrorCode() + ", errInfo:"
                            + amapLocation.getErrorInfo());
                }
            }

        }
    };

    @Override
    public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {

    }
    @Override
    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
    }
}
